/**
 * 登录
 */
import * as api from '@/services/base/login';

import { routerMock } from '../../utils/base/mock';

export default {
  namespace: 'base_login',

  state: {
    userInfo: {},
  },

  effects: {
    *getCode({ payload }, { call }) {
      return yield call(api.getCode, payload);
    },

    *getUserInfo({ payload }, { call, put }) {
      const { code, user } = yield call(api.getUserInfo, payload);
      yield put({
        type: 'saveState',
        payload: {
          userInfo: user || {},
        },
      });
      return user || {};
    },
    *loginOut({ payload }, { call }) {
      return yield call(api.loginOut);
      // const { code } = yield call(api.loginOut);
      // return code;
    },

    *getGovRouters({ payload }, { call }) {
      const { data } = yield call(api.getGovRouters);
      return data || [];
    },

    *sysLogin({ payload }, { call }) {
      const res = yield call(api.sysLogin, payload);
      let resData = [];
      const { code, msg } = res;
      if (code && code === 200) {
        const {
          data: { access_token },
        } = res;
        sessionStorage.setItem('author', access_token);
        const { data } = yield call(api.getGovRouters);
        // resData = data || [];
        resData = routerMock.data
        sessionStorage.setItem('menuData', JSON.stringify(resData));
      }
      return { code, msg, resData };
    },
  },

  reducers: {
    saveState(state, { payload }) {
      return { ...state, ...payload };
    },
  },
};
